package com.glory.huaweipda.view

import android.annotation.SuppressLint
import android.app.ActivityManager
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.os.Message
import android.util.Log
import android.view.View
import android.widget.TextView
import com.afollestad.materialdialogs.MaterialDialog
import com.afollestad.materialdialogs.customview.customView
import com.afollestad.materialdialogs.customview.getCustomView
import com.afollestad.materialdialogs.lifecycle.lifecycleOwner
import com.glory.huaweipda.R
import com.glory.huaweipda.adapter.SimpleAdapter
import com.glory.huaweipda.base.MyApp
import com.glory.huaweipda.bean.EqpLogBean
import com.glory.huaweipda.bean.LotInfoBean
import com.glory.huaweipda.viewmodel.ScanStartModel
import com.glory.mvvmnet.base.BaseActivity
import kotlinx.android.synthetic.main.activity_log.*
import kotlinx.android.synthetic.main.activity_scan_start.*
import kotlinx.android.synthetic.main.activity_scan_start.arrival
import kotlinx.android.synthetic.main.activity_scan_start.batch_code
import kotlinx.android.synthetic.main.activity_scan_start.eqpState
import kotlinx.android.synthetic.main.activity_scan_start.equipment_code
import kotlinx.android.synthetic.main.activity_scan_start.ivBack
import kotlinx.android.synthetic.main.activity_scan_start.nextStepName
import kotlinx.android.synthetic.main.activity_scan_start.priority
import kotlinx.android.synthetic.main.activity_scan_start.recipeDesc
import kotlinx.android.synthetic.main.activity_scan_start.recipeName
import kotlinx.android.synthetic.main.activity_scan_start.recycler_scan
import kotlinx.android.synthetic.main.activity_scan_start.stepName
import kotlinx.android.synthetic.main.activity_scan_start.title_text
import kotlinx.android.synthetic.main.activity_system_set.*
import kotlinx.android.synthetic.main.activity_trackout.*

@Suppress("CAST_NEVER_SUCCEEDS")
class TrackInActivity : BaseActivity<ScanStartModel>() {

    var eqpID: String = ""
    var user: String = ""
    var lotID: String = ""

    var type: String = ""
    lateinit var lotInfoBean:LotInfoBean

    var handler: Handler = object : Handler() {
        @SuppressLint("HandlerLeak")
        override fun handleMessage(msg: Message) {
            super.handleMessage(msg)
            when (msg.what) {
                0x10 -> {
                    arrival.setEnabled(true)
                    var s: String = msg.obj as String
                    type = "oo"
                    val intent = Intent()
                    intent.putExtra("showPlugin", "errorLog")
                    intent.putExtra("error", s)
                    val clazz = Class.forName("com.glory.huaweipda.view.SkipDialogActivity")
                    intent.setClass(this@TrackInActivity, clazz)
                    startActivity(intent)
                }
            }
        }
    }

    override fun layoutId(): Int {
        return R.layout.activity_scan_start
    }

    override fun initView(savedInstanceState: Bundle?) {

        val simpleAdapter = SimpleAdapter(this, viewModel.mfgItems)
        recycler_scan.adapter = simpleAdapter
        val instance = MyApp.instance
        instance!!.handler = handler
//        val bind: ActivityScanStartBinding =
//            DataBindingUtil.setContentView(this, R.layout.activity_scan_start)
        recycler_scan.setNestedScrollingEnabled(false)
        eqpID = intent.getStringExtra("eqpID").toString()
        user = intent.getStringExtra("user").toString()
        lotID = intent.getStringExtra("lotID").toString()
        lotInfoBean = intent.getSerializableExtra("lotsBean") as LotInfoBean
        title_text.text = "开始作业"
//        viewModel.defaultLotInfo(eqpID, user, lotID)
        equipment_code.setText(eqpID)
        batch_code.setText(lotID)
        val lotsBean = lotInfoBean.lots[0]
        eqpState.setText(lotsBean.eqpState)
        recipeName.setText(lotsBean.recipeName)
        recipeDesc.setText(lotsBean.recipeDesc)
        stepName.setText(lotsBean.stepName)
        priority.setText(lotsBean.priority)
        nextStepName.setText(lotsBean.nextStepName)
        viewModel.defaultGetEQPLog(eqpID)

//        bind.scanStartModel = viewModel
        viewModel.initAssets(this)

        arrival.setOnClickListener {
            arrival.setEnabled(false)
            viewModel.defaultTrackIn(eqpID, user, lotID)
        }

        viewModel.defUI.callObserve.observe(this) {

            if (type.equals("oo")) it.type = -1
            when (it.type) {
                0 -> {

                }
                1 -> {
                    val eqpLogBean = it.item as EqpLogBean
                    for (i in 0 until eqpLogBean.logmessagetextlist.size - 1) {
                        if (eqpLogBean.logmessagetextlist[i].isalert.equals("1")){
                            showDialogs(eqpLogBean.logmessagetextlist[i].logmessagetext,eqpLogBean.logmessagetextlist[i].messageid.toString())
                        }
                    }
                    simpleAdapter.notifyDataSetChanged()

                    viewModel.defaultGetEQPLog(eqpID)
                }
                200 -> {
//                    val intent = Intent()
//                    intent.putExtra("showPlugin", "arrival")
//                    intent.putExtra("track", "track")
//                    val clazz = Class.forName("com.glory.huaweipda.view.SkipDialogActivity")
//                    intent.setClass(this, clazz)
//                    startActivityForResult(intent, 1)
                }
            }
        }


        ivBack.setOnClickListener {
            finish()
        }
    }

    private var currentID = arrayListOf<String>()
    private fun showDialogs(logmessagetext: String,id:String) {
        if(currentID.contains(id)){
            return
        }
        currentID.add(id)
        val dialog = MaterialDialog(this)
            .cancelable(false)
            .cornerRadius(12f)
            .customView(R.layout.dialog_track_out_start, noVerticalPadding = true)
            .lifecycleOwner(this)
        val contentView = dialog.getCustomView()
        val tips_text: TextView = contentView.findViewById(R.id.tips_text)
        val set_no: TextView = contentView.findViewById(R.id.set_no)
        set_no.visibility = View.GONE
        tips_text.text = logmessagetext
        dialog.show()

    }

    override fun initData() {

    }

    fun getTopActivity(context: Context): String {
        val am = context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
        val cn = am.getRunningTasks(1)[0].topActivity
        Log.d("Chunna.zheng", cn!!.className) //包名加类名
        return cn.className
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)

        if (requestCode == 1) {
            when (resultCode) {
                2 -> {
                    val extras = data!!.getExtras()
                    val again = extras!!.getString("again")
                    finish()
                }

            }

        }

    }

}